package com.imooc.activity7_workflow;

import org.activiti.engine.RepositoryService;
import org.activiti.engine.repository.Deployment;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.InputStream;
import java.util.List;
import java.util.zip.ZipInputStream;

@SpringBootTest
class Part1_Deployment {

    @Autowired
    private RepositoryService repositoryService;

    /**
     * 通过bpmn部署流程
     */
    @Test
    void initDeploymentBPMN() {
        String filename = "BPMN/Part4_Task_claim.bpmn";
//        String pngname = "BPMN/Part4_Task.bpmn";
        Deployment deployment = repositoryService.createDeployment()
                .addClasspathResource(filename)
//                .addClasspathResource(pngname)
                .name("候选人流程部署001")
                .deploy();

        System.out.println(deployment.getName());
    }

    /**
     * 通过zip部署流程
     */
    @Test
    public void initDeploymentZIP(){
        InputStream fileInputStream = this.getClass()
                .getClassLoader()
                .getResourceAsStream("BPMN/bpmn&svg.zip");

        assert fileInputStream != null;
        ZipInputStream zip = new ZipInputStream(fileInputStream);
        Deployment deployment = repositoryService.createDeployment()
                .addZipInputStream(zip)
                .name("流程部署测试ZIP")
                .deploy();

        System.out.println(deployment.getName());
    }

    /**
     * 查询流程部署
     */
    @Test
    public void getDeployments(){
        List<Deployment> list = repositoryService.createDeploymentQuery()
                .list();
        list.forEach(deployment -> {
            System.out.println("ID：" + deployment.getId());
            System.out.println("NAME：" + deployment.getName());
            System.out.println("DeploymentTime：" + deployment.getDeploymentTime());
            System.out.println("key：" + deployment.getKey());
        });
    }
}
